extends Node
func _ready():
	pass
func _init():
	call_deferred("init")
func _connect():
	pass
var utils = null
var azurCtrl = null
var base = null
var path = null
var window
var recordWindow
var config
var buff
func init():
	if globalData.infoDs.has("g_azurlaneBase"):
		base = globalData.infoDs["g_azurlaneBase"]
	if base != null:
		#初始化
		utils = globalData.infoDs["g_azurlaneUtils"]
		azurCtrl = globalData.infoDs["g_azurlane"]
		buff = globalData.infoDs["g_azurlaneBuffs"]
		base.connect("onSaveGame",self,"onSaveGame")
		azurCtrl.connect("onNewGame",self,"newGame")
		azurCtrl.connect("resetData", self, "resetData")
		azurCtrl.connect("onLoadGame",self,"loadGame")
		yield(sys.get_tree().create_timer(0.2), "timeout")
		config = load("%s/g_azurlaneRelic/RelicConfig.gd" % [sys.getBasePath()]).new()

func onSaveGame():
	var tmp = {}
	tmp["shieldBonus"] = shieldBonus
	tmp["betterFreeze"] = betterFreeze
	tmp["betterShaoShi"] = betterShaoShi
	tmp["picked"] = picked
	tmp["louShuiRelic"] = louShuiRelic
	tmp["evoRelic"] = evoRelic
	base.setData("relic", tmp)

func loadGame():
	loadData()
	sys.main.connect("onBattleStart",self,"onBattleStart")

func loadData():
	var tmp = base.getData("relic", {})
	if not tmp.empty():
		shieldBonus = tmp["shieldBonus"]
		betterFreeze = tmp["betterFreeze"]
		betterShaoShi = tmp["betterShaoShi"]
		picked = tmp["picked"]
		louShuiRelic = tmp["louShuiRelic"]
		evoRelic = tmp["evoRelic"]

func newGame():
	sys.main.connect("onBattleStart",self,"onBattleStart")

func resetData():
	window = null
	recordWindow = null
	#护盾加成
	shieldBonus = 1
	#霜冻强化
	betterFreeze = false
	#烧蚀强化
	betterShaoShi = false
	evoRelic = false
	louShuiRelic = false


	picked.clear()
	rolled.clear()

func onStartGame():
	pass

func onBattleStart():
	for i in picked:
		if i.get("onBattleStart") != true:continue
		var id = int(i.id)
		match id:
			5:sys.main.player.plusGold(20)
			6:azurCtrl.changeMF(15)

		if i.get("loopAlly") == true:
			for cha in utils.getAllChas(2):
				if id == 7 or id == 8 or id == 9:
					if i.shipType.has(cha.type):
						cha.addBuff(buff.b_atkR.new(3.5, 0, false))
				elif id == 10 or id == 11:
					if i.shipType.has(cha.type):
						cha.addBuff(buff.b_cd.new(3.5))
				elif id == 12:
					cha.addBuff(config.b_rel_ywxk.new())
				elif id == 13:
					cha.addBuff(config.b_rel_lzjd.new())
				elif id == 16:
					cha.addBuff(config.b_rel_fltys.new())
				elif id == 24:
					cha.addBuff(config.b_rel_lzgm.new())
				elif id == 30:
					cha.addBuff(config.b_rel_wangzhe.new())
				elif id == 31:
					cha.addBuff(config.b_rel_jstcq.new())
				elif id == 35:
					cha.addBuff(buff.b_wudi.new(3))
				elif id == 40 and cha.type == sys.CharaClass.DD:
					cha.addBuff(config.b_rel_shengJ.new())
				elif id == 43:
					cha.addBuff(config.b_rel_xianliufa.new())
				elif id == 44:
					cha.addBuff(config.b_rel_wangpaijz.new())

		if i.get("loopEnemy") == true:
			for cha in utils.getAllChas(1):
				if id == 15 and cha.type == sys.CharaClass.SIREN:
					cha.forceHurtSelf(cha.att.maxHp*0.4)

		if id == 26:
			var chas = []
			for j in utils.getAllChas(1):
				if j.camp != "塞壬":chas.append(j)
			chas.sort_custom(sys, "sortByLvDesc")
			chas[0].forceHurtSelf(chas[0].att.maxHp)

func onBattleReady():
	# if sys.main.batType == 2 and diff.step < 100:
	# 	open()
	pass
	
func showRecord():
	if not is_instance_valid(recordWindow) or not recordWindow is WindowDialog:
		recordWindow = utils.createWindowDialog(sys.main.get_node("ui"), "当前遗物", Vector2(700, 450))
		var vbox1 = utils.createVBox(recordWindow)
		vbox1.rect_position = Vector2(30, 100)
		for j in picked:
			utils.createRichTextLabel("[color=#DC143C]%s[/color] - %s"%[j.name, j.desc], vbox1, Vector2(650, 50))
	recordWindow.popup_centered()

var rolled = []
var picked = []
func initUi():
	window = utils.createWindowDialog(sys.main.get_node("ui"), "选择遗物", Vector2(1100, 400))
	window.popup_exclusive = true
	window.get_close_button().hide()

	var hbox = utils.createHBox(window)
	hbox.rect_position = Vector2(30, 70)
	hbox.name = "hbox"

func open():
	if not is_instance_valid(window) or not window is WindowDialog:
		initUi()
	var hbox = window.get_node("hbox")
	for i in hbox.get_children():
		i.queue_free()
	rollRelic()
	# hbox.set_alignment(1)
	for i in rolled:
		var itemPanel = Panel.new()
		itemPanel.name = "panel"
		itemPanel.rect_min_size = Vector2(350, 250)
		hbox.add_child(itemPanel)
		# var img = utils.createTextureRect("g_azurlineRelic/" + i.ico)
		# itemPanel.add_child(img)
		# img.rect_position = Vector2(140, 50)

		utils.createRichTextLabel("[color=#BDB76B]" + i.name, itemPanel, Vector2(250, 50), Vector2(125, 40))
		utils.createRichTextLabel(i.desc, itemPanel, Vector2(300, 150), Vector2(30, 90))
		utils.createItemButton(itemPanel, self, "   选择   ", "doGetRelic", [i], Vector2(125, 180))

	window.popup_centered()	

func doGetRelic(conf):
	if conf.get("instant") == true:
		afterGetRelic(conf)
	picked.append(conf)
	window.hide()
	var hbox = window.get_node("hbox")
	for i in hbox.get_children():
		i.queue_free()

#护盾加成
var shieldBonus:float = 1
#霜冻强化
var betterFreeze:bool = false
#烧蚀强化
var betterShaoShi:bool = false
#漏水遗物
var louShuiRelic:bool = false
#进化相关遗物
var evoRelic:bool = false

func afterGetRelic(conf):
	match conf.id:
		1:shieldBonus = conf.num
		2:
			var chaId = sys.rndListItem(chaData.upChas).id
			chaId = chaData.getLvIds(chaId, 3)
			sys.main.player.addCha(sys.main.newChara(chaId))
		3:betterFreeze = true
		4:betterShaoShi = true
		20:utils.rollItem(0, 1, "获得角色", 1000)
		41:louShuiRelic = true
		
func rollRelic():
	rolled.clear()
	for i in range(3):
		var bf = sys.rndListItem(config.buffTemplates)
		while rolled.has(bf) or picked.has(bf):
			bf = sys.rndListItem(config.buffTemplates)
		rolled.append(bf)
	
		

